#!/bin/sh
# 启动 redis 并监听6379
# sh run redis 6379

# 启动 redis 并监听 6379 - 6380
# sh run redis 6379 -6380

# 启动 cluster 并监听 6379 - 6380
# sh run cluster 6379 -6380.

# sh run stop redis 6379 # 停止运行redis 6379

# del node file
# sh run dnf redis 6379 # 停止运行redis 6379

# 当前运行的绝对路径
basepath=$(cd `dirname $0`; pwd)


# 当前ip

ip=`ifconfig enp0s31f6|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

# docker ps -a|grep redis  |awk '{print $1}'|xargs docker stop >/dev/null 2>&1
# docker ps -a|grep redis |awk '{print $1}'|xargs docker rm >/dev/null 2>&1

# /usr/local/etc/redis/redis.conf
# 数据基本目录
base_dir=/tmp/data

obj=redis
if [ "$1empty" != "empty" ]; then
    obj=$1
fi



# 开始端口
start_port=6379
end_port=6379


if [ "${obj}empty" = "stopempty" ]; then

    if [ "$2empty" != "empty" ]; then
        obj=$2
    fi

    if [ "$3empty" != "empty" ]; then
        start_port=$3
        end_port=$3
    fi

    if [ "$4empty" != "empty" ]; then
        end_port=$4
    fi

    # 停止运行
    for port in `seq $start_port $end_port`
    do
        # 容器名称
        name="redis_${obj}_$port"
        echo  "stop $name \n"
        # 清除旧容器
        docker ps -a|grep $name |awk '{print $1}'|xargs docker stop
        docker ps -a|grep $name |awk '{print $1}'|xargs docker rm
    done
    echo "stoped";
    exit;
fi

# del node file
if [ "${obj}empty" = "dnfempty" ]; then

    if [ "$2empty" != "empty" ]; then
        obj=$2
    fi
    base_dir=$base_dir/$obj

    if [ "$3empty" != "empty" ]; then
        start_port=$3
        end_port=$3
    fi

    if [ "$4empty" != "empty" ]; then
        end_port=$4
    fi

    # 停止运行
    for port in `seq $start_port $end_port`
    do
        # 容器名称
        name="redis_${obj}_$port"
        # 端口目录地址
        port_dir=$base_dir/$port
        data_dir=$port_dir/data

        node_file=$data_dir/nodes.conf
        echo "rm $node_file";
        rm $node_file;
    done
    echo "deled";
    exit;
fi




# 启动程序



base_dir=$base_dir/$obj

if [ "$2empty" != "empty" ]; then
    start_port=$2
    end_port=$2
fi

if [ "$3empty" != "empty" ]; then
    end_port=$3
fi


# 修改文件权限
# find 60*/data|grep -v git|xargs sudo chown 100:101

# 源配置地址

sourceDir=$basepath/$obj;

if [ ! -d "$sourceDir" ];then
    echo 'source conf not found';
    exit;
fi

replicas=""
for port in `seq $start_port $end_port`
do
    # 容器名称
    name="redis_${obj}_$port"
    # 清除旧容器
    docker ps -a|grep $name |awk '{print $1}'|xargs docker stop >/dev/null 2>&1
    docker ps -a|grep $name |awk '{print $1}'|xargs docker rm >/dev/null 2>&1

    # 端口目录地址
    port_dir=$base_dir/$port
    data_dir=$port_dir/data
    # 分片数据
    if [ "${replicas}empty" = 'empty' ]; then
        replicas="$ip:$port"
    else
        replicas="$replicas;$ip:$port"
    fi


    # 目录创建
    if [ ! -d "$port_dir" ];then
        mkdir $port_dir -p
        cp $sourceDir/redis.conf $port_dir
        sed -i 's/^port.*/port '$port'/g' $port_dir/redis.conf
    fi
    if [ ! -d "$data_dir" ];then
        mkdir $data_dir -p
    fi
    # 权限管理
    chown 100:101 $data_dir

    # 运行容器
    docker run \
    -d \
    --user redis\
    --name $name \
    -p $port:$port \
    -p 1$port:1$port \
    --net host \
    --restart=always \
    -v $data_dir:/data \
    -v $port_dir/redis.conf:/cluster/redis.conf \
    wolferhua/redis:4 redis-server /cluster/redis.conf

done
# docker run  -it  wolferhua/redis:4 /cluster/redis-trib.rb create --replicas 1 $replicas

# sudo chown wolferhua:wolferhua 60* -Rf
# find |grep nodes|xargs sudo rm
# docker run  -it  wolferhua/redis:4 redis-cli -c -h 10.0.7.29 -p 6001
if [ $obj = 'cluster' ]; then
   echo  "docker run  -it  -e REDIS_HOST=$ip -e REDIS_PORT=$start_port wolferhua/redis:clu-java create \"$replicas\""
fi
